--[[
   @brief    新版社交客户端承载
   @author   senpingtian
   @version  1.0
   @date     2019-11-08
--]]

pwg_social_view = {
    
    DIALOG_LAYOUT = "wegame_social\\NewSocialDialog.tpf";

    kCloseBtnMargin = { -1, 7, 16, -1 }; -- -1:ignore

    dialog_id = Tenio.INVALID_CONTROLID;
    dialog_ptr = nil;

    close_btn_ctn_id = Tenio.INVALID_CONTROLID;
    close_btn_ctn_ptr = nil;

    last_width = 0;
    last_height = 0;

    should_show_close_btn = true;

    startup_url = LoadStr("SOCIAL_MAIN_PAGE_URL");
};

function pwg_social_view:log(msg, core_msg)
    base_utility.log("[pwg_social_view]" .. msg, core_msg);
end;

function pwg_social_view:init()
    self:log("init.");
end;

function pwg_social_view:create_window(url)
    if nil ~= self.dialog_ptr then
        return self.dialog_ptr;
    end
    local dlg_id = layout_mgr.restore_layout(self.DIALOG_LAYOUT);
    if Tenio.INVALID_CONTROLID == dlg_id then
        self:log("restore layout failed.", true);
        return;
    end
    self.dialog_id = dlg_id;
    local close_btn_dlg_id = ui_utility.get_ctrl_id_by_path(dlg_id, "CtnCloseBtn");
    if Tenio.INVALID_CONTROLID == close_btn_dlg_id then
        self:log("get close btn dlg failed.", true);
        return;
    end
    self.close_btn_ctn_id = close_btn_dlg_id;
    local close_btn_dlg_ptr = ui_utility.get_ctrl_ptr_by_id(close_btn_dlg_id);
    if nil == close_btn_dlg_ptr then
        self:log("get close btn dlg ptr failed.", true);
        return;
    end
    self.close_btn_ctn_ptr = close_btn_dlg_ptr;

    ui_utility.setproperty_by_id(self.dialog_id, "OnPosChanged", "pwg_social_view:on_pos_changed");

    local dlg_ptr = ui_utility.get_ctrl_ptr_by_id(dlg_id);
    if nil == dlg_ptr then
        self:log("get dlg handle failed.", true);
        return;
    end
    self.dialog_ptr = dlg_ptr;
    local webctrl_id = ui_utility.get_ctrl_id_by_path(dlg_id, "QBWebCtrl");
    if Tenio.INVALID_CONTROLID == webctrl_id then
        self:log("get webctrl failed,", true);
        return;
    end
    ui_utility.setproperty_by_id(webctrl_id, "OnNavigateFailed", 
                                "pwg_social_view:on_web_navigate_failed");
    ui_utility.setproperty_by_id(webctrl_id, "OnNavigateComplete", 
                                "pwg_social_view:on_web_navigate_complete");
    local loading_url = self.startup_url .. "?from=client";
    if nil ~= url and "" ~= url then
        loading_url = url;
    end
    self.should_show_close_btn = true;
    self:load_page(loading_url);
    base_utility.add_interval("pwg_social_view:delay_show_social_window", 2000);
    return dlg_ptr, true;
end;

function pwg_social_view:show_window()
    if nil ~= self.dialog_ptr then
        if not self:is_window_visible() then
            self.dialog_ptr:Restore();
        end
        self.dialog_ptr:Show(TRUE);
        self.dialog_ptr:BringToTop();
    end

    if self.should_show_close_btn then
        self:show_close_button(true);
    end
end

function pwg_social_view:hide_window()
    if nil ~= self.dialog_ptr then
        self.dialog_ptr:Show(FALSE);
    end
    self:close_window();
end

function pwg_social_view:close_window()
    --shadow_helper.destroy_shadow(self.dialog_id);
    if Tenio.INVALID_CONTROLID ~= self.dialog_id then
        layout_mgr.delay_remove_ctrl(self.dialog_id);
    end
    self.dialog_id = Tenio.INVALID_CONTROLID;
    self.dialog_ptr = nil;
    self.close_btn_ctn_id = Tenio.INVALID_CONTROLID;
    self.close_btn_ctn_ptr = nil;
end

function pwg_social_view:is_window_visible()
    if nil == self.dialog_ptr then
        return false;
    end
    local ww, wh = ui_utility.get_control_size_by_id(self.dialog_id);
    if 0 == ww or 0 == wh then
        return false;
    end
    if self.dialog_ptr:IsVisible() == TRUE then
        return true;
    end
    return false;
end;

function pwg_social_view:load_page(url)
    if nil == url then return end;
    local ptr = ui_utility.get_ctrl_ptr_by_cid(CID_WEB_CTRL_FOR_NEW_SOCIAL);
    if nil == ptr then
        self:log("cannot get webctrl, load page failed.", true);
    end
    local str_url, str_holder = U8toCUIStr(url);
    ptr:Navigate(str_url);
end;

function pwg_social_view:on_web_navigate_failed()
    self:show_close_button(false);
end;

function pwg_social_view:on_web_crashed()
    self:show_close_button(true);
end;

function pwg_social_view:on_web_navigate_complete(uilib, page_url)
    if Tenio.INVALID_CONTROLID ~= self.dialog_id then
        local loading_ani_ctrl = ui_utility.get_ctrl_id_by_path(self.dialog_id, "CntLoading");
        ui_utility.setproperty_by_id(loading_ani_ctrl, "Visible", "false");
        self:show_window();
        if nil ~= page_url then
            if nil ~= string.find(page_url, "loadFailed") then
                self:show_close_button(false);
            end
        end
    end
end;

function pwg_social_view:on_pos_changed()
    if nil == self.dialog_ptr then
        return;
    end

    local rc_host = self.dialog_ptr:GetRectEx(FALSE, TRUE);
    local ww, wh = rc_host.right - rc_host.left, rc_host.bottom - rc_host.top;
    if nil ~= self.close_btn_ctn_ptr then
        local rc_btn = self.close_btn_ctn_ptr:GetRectEx(FALSE, TRUE);
        local ww_btn, wh_btn = rc_btn.right - rc_btn.left, rc_btn.bottom - rc_btn.top;
        rc_btn.right = ww - self.kCloseBtnMargin[3];
        rc_btn.left = rc_btn.right - ww_btn;
        rc_btn.top = 0 + self.kCloseBtnMargin[2];
        rc_btn.bottom = rc_btn.top + wh_btn;
        local tpf_ui = base_utility.get_component_adapter("ITpf_ui", "ierd_tgp.tpf_ui.GetTPFUI();");
        if nil ~= tpf_ui then
            tpf_ui:DPIScale(rc_btn);
        end
        self.close_btn_ctn_ptr:Move(rc_btn);
    end

    if ww ~= self.last_width or wh ~= self.last_height then
        self.last_width = ww;
        self.last_height = wh;
        local params = {
            left = rc_host.left;
            top = rc_host.top;
            width = ww;
            height = wh;
        };
        comm_center.send_broadcast("i_broadcast_new_social_window_size_changed", params);
    end
end;

function pwg_social_view:delay_show_social_window()
    base_utility.remove_interval("pwg_social_view:delay_show_social_window");
    self:show_window();
end;

function pwg_social_view:show_close_button(show)
    if nil == self.close_btn_ctn_ptr then
        return;
    end
    if show then
        self.close_btn_ctn_ptr:Show(TRUE);
    else
        self.close_btn_ctn_ptr:Show(FALSE);
    end
end;

function pwg_social_view:hide_close_button()
    self.should_show_close_btn = false;
    self:show_close_button(false);
end;

